package com.huanuo.internetofthings.service.impact.statistics;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.unicorn.framework.core.ResponseDto;
import org.unicorn.framework.core.exception.PendingException;

import com.huanuo.internetofthings.bo.impact.ImpactMonthStatistics;
import com.huanuo.internetofthings.bo.user.UserInfo;
import com.huanuo.internetofthings.dto.impact.ImpactDeviceStatisticsReqDto;
import com.huanuo.internetofthings.enums.ImpactDeviceStatisticsType;

/**
 * 设备开工同环比增长率分布
 * 
 * @author xiebin
 *
 */
@Service
public class ImpactDeviceCommencementYearOnYearRateStatisticsService extends AbstractImpactDeviceStatisticsService {

	@Override
	public ImpactMonthStatistics getImpactMonthStatistics(UserInfo userInfo) throws PendingException {
		ImpactMonthStatistics impactMonthStatistics = new ImpactMonthStatistics();
		impactMonthStatistics.setGroupNo(userInfo.getGroupNo());
		int year = getYear();
		impactMonthStatistics.setYear(year);
		return impactMonthStatistics;
	}

	@Override
	public ResponseDto<?> calculationStatistics(List<ImpactMonthStatistics> impactMonthStatisticsList,UserInfo userInfo)
			throws PendingException {
		Map<Integer, BigDecimal>   curYearStatisticMap =getCurYearStatisticMap(impactMonthStatisticsList);
		Map<Integer, BigDecimal>   lastYearStatisticMap =getLastYearStatisticMap(userInfo);
		return new ResponseDto<>(calculationYearOnYearStatistics(lastYearStatisticMap,curYearStatisticMap));
	}

	/**
	 * 计算同比
	 * @param lastYearStatisticMap
	 * @param curYearStatisticMap
	 * @return
	 * @throws PendingException
	 */
	private Map<Integer, BigDecimal>  calculationYearOnYearStatistics(Map<Integer, BigDecimal> lastYearStatisticMap,Map<Integer, BigDecimal> curYearStatisticMap)throws PendingException{
		Map<Integer, BigDecimal>  returnMap=getSortMap(BigDecimal.class);
		Set<Integer> monthSet=lastYearStatisticMap.keySet();
		for(Integer m:monthSet){
			BigDecimal curYearM=curYearStatisticMap.get(m);
			BigDecimal lastYearM=lastYearStatisticMap.get(m);
			returnMap.put(m, (curYearM.subtract(lastYearM)).multiply(new BigDecimal(100)).divide(lastYearM, 2, BigDecimal.ROUND_HALF_UP));
		}
		return returnMap;
	}
    /**
     * 获取上一年统计情况
     * @param userInfo
     * @return
     * @throws PendingException
     */
	private Map<Integer, BigDecimal> getLastYearStatisticMap(UserInfo userInfo) throws PendingException{
		int year = getYear();
		ImpactMonthStatistics impactMonthStatistics= new ImpactMonthStatistics();
		impactMonthStatistics.setYear(year-1);
		impactMonthStatistics.setGroupNo(userInfo.getGroupNo());
		// 根据用户所属组查询上一年12月份汇总信息
		List<ImpactMonthStatistics> impactMonthStatisticsList = impactDeviceMonthStatisticsService
				.getImpactMonthStatisticsList(impactMonthStatistics);
		return getCurYearStatisticMap(impactMonthStatisticsList);
	}
	/**
	 * 获取今年统计情况
	 * @param impactMonthStatisticsList
	 * @return
	 * @throws PendingException
	 */
	private Map<Integer, BigDecimal> getCurYearStatisticMap(List<ImpactMonthStatistics> impactMonthStatisticsList) throws PendingException{
		// 按月分组
		Map<Integer, List<ImpactMonthStatistics>> mapGroupbyMonth = impactMonthStatisticsList.stream()
				.collect(Collectors.groupingBy(ImpactMonthStatistics::getMonth));
		 //初始化统计结果
		Map<Integer, BigDecimal> curYearStatisticMap = getSortMap(BigDecimal.class);
		for (int m = 1; m <= 12; m++) {
			curYearStatisticMap.put(m, new BigDecimal(0));
		}
		Set<Integer> set = mapGroupbyMonth.keySet();
		for (Integer mm : set) {
			Integer deviceTotal = mapGroupbyMonth.get(mm).stream().mapToInt(ImpactMonthStatistics::getDeviceTotal)
					.sum();
			Integer commencementTotal = mapGroupbyMonth.get(mm).stream()
					.mapToInt(ImpactMonthStatistics::getCommencementTotal).sum();
			BigDecimal rate = new BigDecimal(commencementTotal).multiply(new BigDecimal(100))
					.divide(new BigDecimal(deviceTotal),2, BigDecimal.ROUND_HALF_UP);
			curYearStatisticMap.put(mm, rate);
		}
		return curYearStatisticMap;
	}
	
	@Override
	public boolean supports(ImpactDeviceStatisticsReqDto s) throws PendingException {
		return ImpactDeviceStatisticsType.Month_CommencementYearOnYearRateDistribution.equals(s.getImpactDeviceStatisticsType());
	}

}